clear all
set more off
tempfile temp

** PATHS
local working ../working
local ubigeo ../ubigeo

** DATA
use actos_est_new

*keep if inlist(idtipoac,"LDS","MAE","MAT","MEF")

local best = 2001
gen dd = substr(ubidist,-2,2)
gen ubigeo`best' = ubidist
replace ubigeo`best' = substr(ubigeona,1,6) if dd == "00"
destring ubigeo`best', replace

drop _merge
merge m:1 ubigeo`best' using `ubigeo'/ubigeo`best'to1993
drop ubigeo`best'
drop if _merge == 2
drop if _merge == 1
drop _merge

gen year = ano
gen month = year(fechamed)

gen events = 1
gen events_fatal = inlist(idtipoac,"LDS","MAE","MAT","MEF")
gen events_nonfatal = events - events_fatal

gen terrorists = e00 + e01 + e02
replace terrorists = terrorists > 0 if terrorists != . 
gen government = estado

gen terrorists_fatal = terrorists if events_fatal == 1
gen terrorists_nonfatal = terrorists if events_nonfatal == 1

gen government_fatal = government if events_fatal == 1
gen government_nonfatal = government if events_nonfatal == 1

keep if government == 1 | terrorists == 1

collapse (sum) events*, by(ubigeo1993 year) // so far without distinguishing perpetrator

rename ubigeo1993 ubigeonac1993
rename year yearviolence

gen any = events > 0

collapse (max) any, by(ubigeonac1993)

** SAVE
save violenceAT_district, replace
